

#include "stdafx.h"
#include "pf_patternlist.h"

#include "pf_system.h"

PF_PatternList* PF_PatternList::uniqueInstance = NULL;



/**
 * Default constructor.
 */
PF_PatternList::PF_PatternList() {
    //patternListListeners.setAutoDelete(false);
}



/**
 * Initializes the pattern list by creating empty PF_Pattern 
 * objects, one for each pattern that could be found.
 */
void PF_PatternList::init() {
    PF_DEBUG->print("PF_PatternList::initPatterns");

    QStringList list = PF_SYSTEM->getPatternList();
    PF_Pattern* pattern;

	patterns.clear();

    for (QStringList::Iterator it = list.begin();
            it != list.end(); ++it) {
        PF_DEBUG->print("pattern: %s:", (*it).toLatin1().data());

        QFileInfo fi(*it);
        pattern = new PF_Pattern(fi.baseName().toLower());
        patterns.append(pattern);

        PF_DEBUG->print("base: %s", pattern->getFileName().toLatin1().data());
    }
}



/**
 * Removes all patterns in the patternlist.
 */
void PF_PatternList::clearPatterns() {
    while (!patterns.isEmpty())
        delete patterns.takeFirst();
}



/**
 * Removes a pattern from the list.
 * Listeners are notified after the pattern was removed from 
 * the list but before it gets deleted.
 */
void PF_PatternList::removePattern(PF_Pattern* pattern) {
    PF_DEBUG->print("PF_PatternList::removePattern()");

    // here the pattern is removed from the list but not deleted
    patterns.removeOne(pattern);

    //for (uint i=0; i<patternListListeners.count(); ++i) {
    //    PF_PatternListListener* l = patternListListeners.at(i);
    //    l->patternRemoved(pattern);
    //}
}



/**
 * @return Pointer to the pattern with the given name or
 * \p NULL if no such pattern was found. The pattern will be loaded into
 * memory if it's not already.
 */
PF_Pattern* PF_PatternList::requestPattern(const QString& name) {
    PF_DEBUG->print("PF_PatternList::requestPattern %s", name.toLatin1().data());

    QString name2 = name.toLower();
    PF_Pattern* foundPattern = NULL;

    PF_DEBUG->print("name2: %s", name2.toLatin1().data());

    // Search our list of available patterns:
    for (int i = 0; i < patterns.size(); ++i) {
        PF_Pattern* p = patterns.at(i);

        if (p->getFileName()==name2) {
            // Make sure this pattern is loaded into memory:
            p->loadPattern();
            foundPattern = p;
            break;
        }
    }

    //if (foundPattern==NULL && name!="standard") {
    //    foundPattern = requestPattern("standard");
    //}

    return foundPattern;
}

	
bool PF_PatternList::contains(const QString& name) {
    QString name2 = name.toLower();

    // Search our list of available patterns:
    for (int i = 0; i < patterns.size(); ++i) {
        PF_Pattern* p = patterns.at(i);

        if (p->getFileName()==name2) {
			return true;
		}
	}

	return false;
}


/**
 * Dumps the patterns to stdout.
 */
std::ostream& operator << (std::ostream& os, PF_PatternList& l) {

    os << "Patternlist: \n";
    for (int i = 0; i < l.patterns.size(); ++i) {
        PF_Pattern* p = l.patterns.at(i);

        os << *p << "\n";
    }

    return os;
}

